//
//  NYRecommendViewModel.m
//  Nooryes
//
//  Created by baken on 2020/2/22.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYRecommendViewModel.h"
#import "NYRecommendManager.h"
#import "NYArticleListModel.h"
#import "NYTopicCategoryModel.h"
#import "NYRecommendSlideModel.h"
#import "NYArticleDetailModel.h"
#import "NSString+dynamicSize.h"
#import "NYCommentListModel.h"
#import "TopicCategoryModel.h"
#import "NYMiniAdModel.h"


@interface NYRecommendViewModel()

@property (nonatomic, assign) NSInteger commentPage;

@end

@implementation NYRecommendViewModel

- (void)getRecommendCategoryWithCompletionHandle:(CompletionHandle)complete{
    [NYRecommendManager getCategoryListWithCompletionHandle:^(NYTopicCategoryModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            [self.topicArray addObjectsFromArray:model.data];
            if (model.data.count > 0) {
                //                TopicCategoryModel *list = model.data[0];
                //                self.topicID = list.talkid;
                TopicCategoryModel *cate = [TopicCategoryModel new];
                cate.name = @"推荐";
                cate.talkid = @"999";
                [self.topicArray insertObject:cate atIndex:0];
            }
        }
        complete(error);
    }];
    
}



- (void)getRecommendSlideWithCompletionHandle:(CompletionHandle)complete{
    [NYRecommendManager getRecommedSlideWithCompletionHandle:^(NYRecommendSlideModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            [self.slideArray removeAllObjects];
            [self.slideImageArray removeAllObjects];
            [self.slideArray addObjectsFromArray:model.data];
            for (RecommendSlideModel *subModel in model.data) {
                [self.slideImageArray addObject:subModel.image];
            }
        }
        complete(error);
    }];
}


- (void)getArticleDetailWithCompletionHandle:(CompletionHandle)complete{
    __block CGFloat topicWidth;
    [NYRecommendManager getArticleWithArticleID:self.articleID andCompletionHandle:^(NYArticleDetailModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            self.articleDetail = model.data;
            model.data.hdwidth = model.data.hdwidth == 0 ? 375 : model.data.hdwidth;
            model.data.hdheight = model.data.hdheight == 0 ? 261 : model.data.hdheight;
            
            self.articleDetail.classWidth = [model.data.classname dynamicWidthWithFontSize:13 andCGSize:CGSizeMake(MAXFLOAT, 15) andLineSpace:0].width +25;
            
            for (TopicCategoryModel *topic in model.data.talks) {
                topic.width = [topic.name dynamicWidthWithFontSize:13 andCGSize:CGSizeMake(MAXFLOAT, 15) andLineSpace:0].width +25;
                topicWidth += topic.width;
            }
        }
        complete(error);
    }];
}


- (void)getRecommendListWithCompletionHandle:(CompletionHandle)complete{
    [NYRecommendManager getRecommedListWithPage:STR_FROM_INT(self.pageNum) andCompletionHandle:^(NYArticleListModel *model, NSError * _Nonnull error) {
        if (!error) {
            if (self.pageNum == 1) {
                [self.dataArr removeAllObjects];
            }
            
            [self.dataArr addObjectsFromArray:model.data];
        }
        complete(error);
    }];
}



- (void)collectArticleWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    if (self.articleDetail.iscollect) {
        [NYRecommendManager cancelCollectInfoWithUserID:uid andSessionID:sessionid andType:self.type andID:self.dataID andCompletionHandle:^(NYMainModel *model, NSError * error) {
            self.articleDetail.iscollect = NO;
            complete(error);
        }];
    } else {
        [NYRecommendManager collectInfoWithUserID:uid andSessionID:sessionid andType:self.type andID:self.dataID andCompletionHandle:^(NYMainModel *model, NSError * error) {
            self.articleDetail.iscollect = YES;
            complete(error);
        }];
    }
}

- (void)postCommentWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    NSString *gb2312Content = [self.content stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *gb2312Title = [self.title stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [NYRecommendManager postCommentWithArticleID:self.articleID andTitle:gb2312Title andContent:gb2312Content andUserID:uid andSessionID:sessionid andCompletionHandle:^(NYMainModel * _Nonnull model, NSError * _Nonnull error) {
        
        
        complete(error);
    }];
}


- (void)replyCommentWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    NSString *gb2312Content = [self.content stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [NYRecommendManager replyCommentWithArticleID:self.articleID andCommentID:self.commentid andContent:gb2312Content andUserID:uid andSessionID:sessionid andCompletionHandle:^(NYMainModel * _Nonnull model, NSError * _Nonnull error) {
        
        
        complete(error);
    }];
}

- (void)getArticleCommentsWithCompletionHandle:(CompletionHandle)complete{
    [NYRecommendManager getArticleCommentsWithArticleID:self.articleID andPage:STR_FROM_INT(self.commentPage) andCompletionHandle:^(NYCommentListModel * _Nonnull model, NSError * _Nonnull error) {
        if (self.commentPage == 1) {
            [self.commentsArray removeAllObjects];
            self.commentHeight = 0;
        }
        
        for (CommentListSubModel *subModel in model.data.list) {
            CGSize contentSize = [subModel.content dynamicWidthWithFontSize:15 andCGSize:CGSizeMake(kScreenWidth-50-24, MAXFLOAT) andLineSpace:0]; //这层主评论的大小
            subModel.contentHeight = contentSize.height;
            if (subModel.replaylist.count == 0) {
                subModel.replyHeight = 0;
                subModel.cellHeight = 67 + contentSize.height + 20;
            } else if (subModel.replaylist.count == 1){
                CommentListSubModel *grandSonModel = subModel.replaylist[0];
                CGSize sonSize = [grandSonModel.content dynamicWidthWithFontSize:15 andCGSize:CGSizeMake(kScreenWidth-40-50-24, MAXFLOAT) andLineSpace:0];
                subModel.replyHeight += sonSize.height + 25;
                subModel.cellHeight = 67 + contentSize.height + 12 + subModel.replyHeight + 30 + 10;
                grandSonModel.contentHeight = [grandSonModel.content dynamicWidthWithFontSize:13 andCGSize:CGSizeMake(kScreenWidth-63, MAXFLOAT) andLineSpace:0].height;
            } else {
                for (int i = 0; i < subModel.replaylist.count; i++) {
                    CommentListSubModel *grandSonModel = subModel.replaylist[i];
                    grandSonModel.contentHeight = [grandSonModel.content dynamicWidthWithFontSize:15 andCGSize:CGSizeMake(kScreenWidth-63, MAXFLOAT) andLineSpace:0].height; //这一条回复的内容高度
                    if (i < 2) {
                        CGSize grandSonSize = [grandSonModel.content dynamicWidthWithFontSize:15 andCGSize:CGSizeMake(kScreenWidth-40-50-24, MAXFLOAT) andLineSpace:0];
                        subModel.replyHeight +=  (grandSonSize.height + 40);
                    }
                }
                if (subModel.replaylist.count > 2) {
                    subModel.replyHeight += 30;
                } else {
                    //                         subModel.replyHeight += 40;
                }
                
                subModel.cellHeight = 67 + contentSize.height + 12 + subModel.replyHeight + 10;
                
            }
            self.commentHeight += subModel.cellHeight;
        }
        if (model.data.count >0) {
            //            self.commentHeight += 30;
        } else {
            self.commentHeight = 150;
            
        }
        [self.commentsArray addObjectsFromArray:model.data.list];
        
        complete(error);
    }];
}

-(void)refreshCommentsCompletionHandle:(CompletionHandle)completionHandle{
    self.commentPage = 1;
    [self getArticleCommentsWithCompletionHandle:completionHandle];
}

-(void)getMoreCommentsCompletionHandle:(CompletionHandle)completionHandle{
    self.commentPage++;
    [self getArticleCommentsWithCompletionHandle:completionHandle];
}


- (void)likeArticleWithCompletionHandle:(CompletionHandle)complete{
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
    NSString *sessionid = [[NSUserDefaults standardUserDefaults]objectForKey:@"sessionid"];
    
    if (self.articleDetail.iszan) {
        [NYRecommendManager cancelLikeInfoWithUserID:uid andSessionID:sessionid andID:self.articleID andCompletionHandle:^(NYMainModel *model, NSError * error) {
            self.articleDetail.iszan = NO;
            complete(error);
        }];
    } else {
        [NYRecommendManager likeInfoWithUserID:uid andSessionID:sessionid andID:self.articleID andCompletionHandle:^(NYMainModel *model, NSError * error) {
            self.articleDetail.iszan = YES;
            complete(error);
        }];
    }
}


- (void)getMiniAdWithCompletionHandle:(CompletionHandle)complete{
    [NYRecommendManager getMiniAdWithCompletionHandle:^(NYMiniAdModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error) {
            self.miniAD = model.data;
        }
        complete(error);
    }];
}

-(void)refreshDataCompletionHandle:(CompletionHandle)completionHandle{
    self.pageNum = 1;
    [self getRecommendListWithCompletionHandle:completionHandle];
}

-(void)getMoreDataCompletionHandle:(CompletionHandle)completionHandle{
    self.pageNum++;
    [self getRecommendListWithCompletionHandle:completionHandle];
}

-(NSMutableArray *)slideArray{
    if (!_slideArray) {
        _slideArray = [NSMutableArray array];
    }
    return _slideArray;
}

-(NSMutableArray *)topicArray{
    if (!_topicArray) {
        _topicArray = [NSMutableArray array];
    }
    return _topicArray;
}

-(NSMutableArray *)slideImageArray{
    if (!_slideImageArray) {
        _slideImageArray = [NSMutableArray array];
    }
    return _slideImageArray;
}

-(NSMutableArray *)commentsArray{
    if (!_commentsArray) {
        _commentsArray = [NSMutableArray array];
    }
    return _commentsArray;
}



/*
 __block NSError *singleError= nil;
 
 dispatch_group_t group = dispatch_group_create();
 
 dispatch_group_enter(group);
 dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 [self getRecommendSlideWithCompletionHandle:^(NSError *error) {
 if (!error) {
 singleError = error;
 }
 NSLog(@"222222222");
 dispatch_group_leave(group);
 }];
 });
 
 dispatch_group_enter(group);
 dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 [self getRecommendCategoryWithCompletionHandle:^(NSError *error) {
 if (!error) {
 singleError = error;
 }
 NSLog(@"11111111");
 }];
 [self getRecommendListWithCompletionHandle:^(NSError *error) {
 if (!error) {
 singleError = error;
 }
 NSLog(@"33333333");
 dispatch_group_leave(group);
 }];
 });
 
 
 dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 NSLog(@"dispatch_group_notify");
 completionHandle(singleError);
 });
 */
@end
